
#include 	"SysConfig.h"
#include	"Define.h"
#include	"Port.h"
#include	"Boot_Uart.h"
#include	"stdarg.h"
#include	"stdio.h"
#include	"Boot_Util.h"

#define	UART6_TX_BUF_SIZE		1024
#define	UART6_RX_BUF_SIZE		32

unsigned char ucUART6_TX_Buf[UART6_TX_BUF_SIZE];
unsigned short usUART6_TX_wPtr;
unsigned short usUART6_TX_sPtr;
unsigned char ucUART6_TX_Active;
unsigned long ulUART6_Timer;

unsigned char ucUART6_RX_Buf[UART6_RX_BUF_SIZE];
unsigned short usUART6_RX_iPtr;
unsigned short usUART6_RX_oPtr;

#ifdef  DEBUG_UART

unsigned char UART6_BufferCnt(void)
{
	if  (usUART6_TX_wPtr == usUART6_TX_sPtr)
	{
		return(0);
	}
	return(1);
}

void UART6_ResetBuf(void)
{
	usUART6_TX_wPtr = 0;
	usUART6_TX_sPtr = 0;
	ucUART6_TX_Active = 0;
	ulUART6_Timer = 0;
}

void UART6_TimerUp(void)
{
	++ulUART6_Timer;
}

void Interrupt_Uart6(void)
{
	if  (usUART6_TX_wPtr == usUART6_TX_sPtr)
	{
	#ifdef  M16C_30PG
		te_u1c1 = 0;
	#else
		te_u6c1 = 0;
	#endif
		ucUART6_TX_Active = 0;
	}
	else
	{
		UART6_TX_SetData();
	}
}

void Interrupt_Uart6RX(void)
{
#ifdef  M16C_30PG
	ucUART6_RX_Buf[usUART6_RX_iPtr] = u1rbl;
#else
	ucUART6_RX_Buf[usUART6_RX_iPtr] = u6rbl;
#endif
	++usUART6_RX_iPtr;
	usUART6_RX_iPtr = usUART6_RX_iPtr % UART6_RX_BUF_SIZE;
}
#endif
unsigned char UART6_TX_SetData(void)
{
#ifdef  M16C_30PG
	u1tbl = ucUART6_TX_Buf[usUART6_TX_sPtr];
#else
	u6tbl = ucUART6_TX_Buf[usUART6_TX_sPtr];
#endif
	++usUART6_TX_sPtr;
	usUART6_TX_sPtr = usUART6_TX_sPtr % UART6_TX_BUF_SIZE;
#ifdef  M16C_30PG
	te_u1c1 = 1;
#else
	te_u6c1 = 1;
#endif
}

unsigned char UART6_TX_SetBuffer(unsigned char ucData)
{
	ucUART6_TX_Buf[usUART6_TX_wPtr] = ucData;
	++usUART6_TX_wPtr;
	usUART6_TX_wPtr = usUART6_TX_wPtr % UART6_TX_BUF_SIZE;

	if  (usUART6_TX_wPtr == usUART6_TX_sPtr)
	{
		return(1);
	}

	return(0);
}

unsigned char ucDBG_NXP_DataCnt;
unsigned char ucDBG_NXP_Buf[32];
unsigned char ucDBG_RX_Mode;

#ifdef  NXP
extern void NXP_I2C_Send(unsigned char *ucBuf, unsigned char ucCnt);
#endif

void UART6_RX_Main(void)
{
	if  (usUART6_RX_iPtr == usUART6_RX_oPtr)
	{
		return;
	}

	switch  (ucDBG_RX_Mode)
	{
		case 0:
			if  (ucUART6_RX_Buf[usUART6_RX_oPtr] == 0xBB)
			{
				ucDBG_RX_Mode = 1;
			}
			++usUART6_RX_oPtr;
			usUART6_RX_oPtr = usUART6_RX_oPtr % UART6_RX_BUF_SIZE;
			break;
		case 1:
			ucDBG_NXP_Buf[ucDBG_NXP_DataCnt] = ucUART6_RX_Buf[usUART6_RX_oPtr];
			++usUART6_RX_oPtr;
			usUART6_RX_oPtr = usUART6_RX_oPtr % UART6_RX_BUF_SIZE;

			if  (ucUART6_RX_Buf[usUART6_RX_oPtr] == 0xAA)
			{
				if  (ucDBG_NXP_DataCnt > 1)
				{
					NXP_I2C_Send(&ucDBG_NXP_Buf[0], ucDBG_NXP_DataCnt);
					LOG_HERO("RV OK");
				}
				ucDBG_NXP_DataCnt = 0;
				ucDBG_RX_Mode = 0;
			}
			else
			{
				++ucDBG_NXP_DataCnt;
			}
			break;
	}
}

unsigned char ucUART6_LastChar;

void UART6_TX_WriteData(const char *format,...)
{
    va_list arg;
    int len;
	unsigned char Buf[100];
	int i;
#if  1
	UART6_TX_SetBuffer(0x0D);
	UART6_TX_SetBuffer(0x0A);
	BOOT_memset(&Buf[0], 0x00, 10);
	sprintf(&Buf[0], "%5d:", ulUART6_Timer * 5);
 	ulUART6_Timer = 0;
	for(i = 0; i < sizeof(Buf); ++i)
	{
		if  (Buf[i] == 0x00)
		{
			break;
		}
		UART6_TX_SetBuffer(Buf[i]);
	}	
#endif
	
    va_start (arg, format);
    len = vsprintf (&Buf[0], format, arg);
    va_end(arg);	
#if  0
	if  (ucUART6_LastChar != *format)	////////
	{									////////
		UART6_TX_SetBuffer(0x0D);		////////
		UART6_TX_SetBuffer(0x0A);		////////
	}									////////
	else
	{
		UART6_TX_SetBuffer(' ');
	}
#endif
	for(i = 0; i < len; ++i)
	{
		UART6_TX_SetBuffer(Buf[i]);
	}

	ucUART6_LastChar = *format;		//////////
	if  (ucUART6_TX_Active == 0)
	{
		ucUART6_TX_Active = 1;
		UART6_TX_SetData();
	}
}

void UART6_Init(void)
{
#ifdef	M16C_64G
    /*--------------------------------------------------------------------------*/
    /* transmit/receive mode register                                           */
    /*--------------------------------------------------------------------------*/
    smd0_u6mr = 1;              /*  SMD2    SMD1    SMD0 */
    smd1_u6mr = 0;              /*  0       0       0   - serial I/O disabled */
    smd2_u6mr = 1;              /*  0       0       1   - clock synchronouse serial I/O mode */
                                /*  0       1       0   - I2C mode */
                                /*  0       1       1   - DO NOT SET */
                                /*  1       0       0   - UART mode transfer data 7 bits long */
                                /*  1       0       1   - UART mode transfer data 8 bits long */
                                /*  1       1       0   - UART mode transfer data 9 bits long */
                                /*  1       1       1   - DO NOT SET */

    /*  internal/external clock select bit */
    ckdir_u6mr = 0;             /*  0 - internal clock */
                                /*  1 - external clock (set port bit to input) */

    /*  stop bit length select bit */
    stps_u6mr = 0;              /*  0 - one stop bit */
                                /*  1 - two stop bits */

    /*  odd/even priority select bit */
    pry_u6mr = 0;               /*  0 - odd parity (active when PRYE = 1) */
                                /*  1 - even parity (active when PRYE = 1) */

    /*  priority enable bit */
    prye_u6mr = 0;              /*  0 - parity disabled */
                                /*  1 - parity enabled */

    /*  TxD/RxD I/O polarity reverse bit */
    iopol_u6mr = 0;             /*  0 - no reverse */
                                /*  1 - reverse */

    /*--------------------------------------------------------------------------*/
    /* transmit/receive control register                                        */
    /*--------------------------------------------------------------------------*/
    /*  BRG count source select */
    clk0_u6c0 = 0;              /*  CLK1    CLK0 */
    clk1_u6c0 = 0;              /*  0       0   - f1SIO or f2SIO selected */
                                /*  0       1   - f8SIO selected */
                                /*  1       0   - f32SIO selected */
                                /*  1       1   - DO NOT SET */

    /*  CTS/RTS function select bit */
    crs_u6c0 = 0;               /*  0 - CTS function selected */
                                /*  1 - RTS function selected */

    /*  CTS/RTS disable bit */
    crd_u6c0 = 0;               /*  0 - CTS/RTS function enabled */
                                /*  1 - CTS/RTS function disabled */

    /*  data output select bit */
    nch_u6c0 = 0;               /*  0 - TxD/SDA and SCL pins CMOS ouput */
                                /*  1 - TxD/SDA and SCL pins N-channel open drain outputs */

    /*  CLK polarity select bit */
    ckpol_u6c0 = 0;             /*  0 - Tx data output on falling edge of transfer clock and Rx data receive on rising edge */
                                /*  1 - Tx data output on rising edge of transfer clock and Rx data receive on falling edge */

    /*  transfer format select bit */
    uform_u6c0 = 0;             /*  0 - LSB first */
                                /*  1 - MSB first */

    u6brg = ( ( 24000000L / 1 / 16 / 115200L ) - 1 );		/* set baud rate */

    /*--------------------------------------------------------------------------*/
    /* special mode register                                                    */
    /*--------------------------------------------------------------------------*/
    u6smr = 0;                  /*  not used in UART mode */

    /*--------------------------------------------------------------------------*/
    /* special mode register 2                                                  */
    /*--------------------------------------------------------------------------*/
    u6smr2 = 0;                 /*  not used in UART mode */

    /*--------------------------------------------------------------------------*/
    /* special mode register 3                                                  */
    /*--------------------------------------------------------------------------*/
    u6smr3 = 0;                 /*  not used in UART mode */

    /*--------------------------------------------------------------------------*/
    /* special mode register 4                                                  */
    /*--------------------------------------------------------------------------*/
    u6smr4 = 0;                 /*  not used in UART mode */

    /*--------------------------------------------------------------------------*/
    /* transmit/receive control register                                        */
    /*--------------------------------------------------------------------------*/
    /*  transmit enable bit */
    te_u6c1 = 0;                /*  0 - transmission disabled */
                                /*  1 - transmission enabled */

    /*  receive enable bit */
    re_u6c1 = 0;                /*  0 - reception disabled */
                                /*  1 - reception enabled */

    /* make P6.3 an output */
    pd1_2 = 0;	// RX
    pd1_3 = 1;	// TX

	s6tic = 0x06;
#endif
}

